# !python3      # 使用工具:PyCharm
# 用户登录名:yyds_l
# 项目名称: python爬虫     文件名称:查找元素的属性
# 系统日期:2022/3/15      系统时间:0:10
# 2022/03/15 00:10

"""ok"""
from bs4 import BeautifulSoup
# import lxml
import requests
dome = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/elsie" class="sister" id="link2"><!-- Elsie --></a>,
<a href="http://example.com/elsie" class="sister" id="link3"><!-- Elsie --></a>,
<a href="http://example.com/elsie" class="sister" id="link4"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister" id="link5">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link6">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...2</p><p class="story">...3
<a class="as1">这是a1</a><a class="as1" id="a2">这是a2</a><a class="as1" href="http://59468.cn">这是a3</a>
</p>
"""


def print_hi():
    # 异常捕捉
    try:
        soup = BeautifulSoup(dome, "html.parser")
        # print(soup.prettify())      # 打印 soup 对象的内容，格式化输出
        # print(soup.a)       # 标签查找
        # print(soup.a.string)        # 获取标签中的文本
        # 判断a的文本是不是注释
        # print(type(soup.a.string))
        # if type(soup.a.string) == bs4.element.Comment:   # 错误！
        #     print(soup.a.string)

        # print(soup.head.contents[0])       # 直接子节点 (以列表形式返回)
        # print(soup.head.children)       # 是一个 list 生成器对象
        # for item1 in soup.body.children:
        #     print(item1)
        """CSS选择器"""
        # print(soup.select('title')[0])          # 标签查找  tag
        # print(soup.select('.sister')[0])         # 通过类名查找  class
        # print(soup.select('#link1')[0])          # 通过id查找  ID
        # print(soup.select('p #link1')[0])        # 组合查找  查找 p 标签中，id 等于 link1的内容
        print(soup.select("body > p > b")[0].text)       # 直接子节点查找  p > b
        # print(soup.select('a[href="http://example.com/elsie"]')[0])    # 属性查找  class / href / src
        # print(soup.select('p a[href="http://example.com/elsie"]')[0])  # 方式组合，不在同一节点的空格隔开，同一节点的不加空格

        for dat_s in soup.select('p #link1'):
            print(dat_s["href"])
        # 多线程调用
        print_hi_two()
        # print(dome)
    except Exception as err:
        print(err)


def print_hi_two():
    # 异常捕捉
    try:
        print()
    except Exception as err:
        print(err)


if __name__ == '__main__':
    print_hi()
